package com.arlin.springcloud.controller;

import com.arlin.springcloud.pojo.Dept;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName: DeptConsumerController
 * @Description: TODO
 * @Author: arlin
 * @Date: 2021/3/21
 */
@RestController
public class DeptConsumerController {

//    private final String REST_URL_PREFIX = "http://localhost:8081";
    //通过Ribbon，这里的访问地址，应该是一个变量，通过服务名来访问
    private final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";

    //简单的Restful服务模板，提供多种便捷访问远程http服务的方法
    private final RestTemplate restTemplate;

    public DeptConsumerController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @RequestMapping("/consumer/dept/add")
    public boolean addDept(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, boolean.class);
    }

    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List<Dept> getAll() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

}
